using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zsa.Emulation.Cartridge;
using RomBroswerControlItem = System.Collections.Generic.KeyValuePair<string, Zsa.Emulation.CNes.UI.RomBrowserForm.CreateRomBrowserHandler>;

namespace Zsa.Emulation.CNes.UI
{
    /// <summary>
    /// The rom browser control
    /// </summary>
    public partial class RomBrowserForm : Form
    {
        #region Fields
        /// <summary>
        /// Used to create the row browser control
        /// </summary>
        /// <returns></returns>
        internal delegate RomBrowserControl CreateRomBrowserHandler();
        /// <summary>
        /// The selected cart.
        /// </summary>
        private CartridgeFileInfo _SelcetedCart = null;
        /// <summary>
        /// The rom source handlers.
        /// </summary>
        private List<KeyValuePair<string, CreateRomBrowserHandler>> _RomSourceHandlers;
        /// <summary>
        /// The current rom browser control
        /// </summary>
        private RomBrowserControl _RomBrowserControl = null;
        #endregion Fields

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="RomBrowserForm"/> class.
        /// </summary>
        public RomBrowserForm()
        {
            InitializeComponent();
            _RomSourceHandlers = new List<KeyValuePair<string, CreateRomBrowserHandler>>(
                new RomBroswerControlItem[] 
                {
                    new RomBroswerControlItem("File System",
                        delegate() { return new FileSystemRomBrowserControl(); }),
                }
            );
            Reset();
            #if DEBUG
            TopMost = false;
            #endif
        }
        #endregion Constructor

        #region Methods
        /// <summary>
        /// Resets this instance.
        /// </summary>
        public void Reset()
        {
            toolStripComboBoxRomSource.Items.Clear();    
            for ( int _ItemIndex = 0; _ItemIndex < _RomSourceHandlers.Count; _ItemIndex++ )
            {
                toolStripComboBoxRomSource.Items.Add(
                    _RomSourceHandlers[_ItemIndex].Key
                );
            }
            toolStripComboBoxRomSource.SelectedIndex = 0;
        }

        /// <summary>
        /// Handles the Click event of the buttonCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// Handles the Click event of the buttonOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }


        /// <summary>
        /// Handles the Click event of the toolStripButtonSettings control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripButtonSettings_Click(object sender, EventArgs e)
        {
            // Show settings path.
        }

        /// <summary>
        /// Handles the Click event of the toolStripButtonSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripButtonSearch_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the toolStripButtonSourceRefresh control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripButtonSourceRefresh_Click(object sender, EventArgs e)
        {
            if (_RomBrowserControl != null)
            {
                _RomBrowserControl.RefreshRoms();
            }
        }



        /// <summary>
        /// The event handler for source combo index changed.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripComboBoxRomSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            // If nothing selected then return.
            if (toolStripComboBoxRomSource.SelectedIndex < 0)
            {
                return;
            }
            // Get the source handler.  An excep can be throw here.
            RomBroswerControlItem _Item = _RomSourceHandlers[toolStripComboBoxRomSource.SelectedIndex];
            // try to create the control.
            RomBrowserControl _Control = null;
            try
            {
                _Control = _Item.Value();
            }
            catch
            {
                throw;
            }
            // If the create delegate returned null then throw n exception.
            if (_Control == null)
            {
                throw new NullReferenceException("The control create handler returned null !");
            }
            Application.DoEvents();
            // Remove the old control.
            panelMain.Controls.Clear();
            // Add the new control.
            panelMain.Controls.Add(
                _Control
                );
            // Set the doc style to fill.
            _Control.Dock = DockStyle.Fill;
            if (_RomBrowserControl != null)
            {
                try
                {
                    // Unsubscribe the old control
                    _RomBrowserControl.CartSelected -= new Action<CartridgeFileInfo>(RomBrowserControl_CartSelected);
                    _RomBrowserControl.Dispose();
                }
                catch { }
            }
            _RomBrowserControl = _Control;
            _RomBrowserControl.CartSelected += new Action<CartridgeFileInfo>(RomBrowserControl_CartSelected);            
            Application.DoEvents();
        }

        /// <summary>
        /// Roms the browser control_ cart selected.
        /// </summary>
        /// <param name="obj">The obj.</param>
        public void RomBrowserControl_CartSelected(CartridgeFileInfo obj)
        {
            _SelcetedCart = obj;
            buttonOk.Enabled = obj != null;
        }
        #endregion Methods

        #region Properties
        /// <summary>
        /// Gets the selected cart.
        /// </summary>
        /// <value>The selected cart.</value>
        public CartridgeFileInfo SelectedCart
        {
            get {
                return _SelcetedCart;
            }
        }


        public virtual object DirectoryKey
        {
            get
            {
                
                return _RomBrowserControl.DirectoryKey;
            }
        }
        #endregion Properties
    }
}